<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class Member extends Model
{
    //软删除
    use SoftDelete;

    //只读字段
    protected $readonly = ['username', 'email'];

    //会员添加
    public function add($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('add')->check($data)) {
            return $validate->getError();
        }
        $result = $this->allowField(true)->save($data);
        if ($result) {
            return 1;
        } else {
            return '会员添加失败';
        }
    }

    //会员编辑
    public function edit($data)
    {
        $validate = new \app\common\validate\Member();

        if (!$validate->scene('edit')->check($data)) {
            return $validate->getError();
        }

        $memberInfo = $this->find($data['id']);
        if ($data['oldpass'] != $memberInfo['password']) {
            return "原密码输入错误";
        }
        $memberInfo['password'] = $data['newpass'];
        $memberInfo['nickname'] = $data['nickname'];
        $result = $memberInfo->save();
        if ($result) {
            return 1;
        } else {
            return '编辑失败！';
        }
    }

    //会员登陆
    public function login($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('login')->check($data)) {
            return $validate->getError();
        }
        $result = $this->where([
            "username" => $data['username'],
            "password" => $data['password']
        ])->find();
        if ($result) {
            session('index',[
                "id" => $result['id'],
                'nickname' => $result['nickname']
            ]);
            cookie('id', $result['id']);
            return 1;
        } else {
            return "用户名或密码错误";
        }
    }

    //会员注册
    public function register($data)
    {
        $validate = new \app\common\validate\Member();
        if (!$validate->scene('add')->check($data)){
            return $validate->getError();
        }
        $result = $this->allowField(true)->save($data);
        if ($result){
            return 1;
        }else{
            return "注册失败";
        }
    }
}
